// index.js
// 获取应用实例
const app = getApp()
const httpUtils = require('../../utils/httpUtils')
const ui = require('../../utils/ui')
Page({
  data: {
    sections: null,
    servers: [
      {
        name: "预约记录",
        icon: "http://images.changzer.cn/%E9%A2%84%E7%BA%A6%E8%AE%B0%E5%BD%95%E6%9F%A5%E8%AF%A2-%E8%93%9D.png",
        url: "../service/record/index"
      },
      {
        name: "体检预约记录",
        icon: "http://images.changzer.cn/体检记录.png",
        url: "../service/test/index"
      },
      {
        name: "就诊卡",
        icon: "http://images.changzer.cn/%E4%BF%A1%E7%94%A8%E5%8D%A1%E9%93%B6%E8%A1%8C%E5%8D%A1.png",
        url: "../service/card/index"
      },
    ],
    articles: [],
    articlePage: 0,
    articlePageSize: 10,
    count: 100,
  },
  onLoad() {
    this.getDepartmentList()
    this.getArticleList()
  },
  getDepartmentList() {
    var _this = this
    var page = { pageNo: 1, pageSize: 9 }
    //发送请求
    httpUtils.request({
      method: "POST",
      showLoading: false,
      url: `/api/department/page`,
      // message:"正在创建...",
      data: page,
    }).then(res => {
      if (res.data.code != 0) {
        ui.showToast(res.data.mes)
      } else {
        _this.setData({
          sections: res.data.data.pageData
        })
      }
    }).catch(err => {
      console.log(err)
    });
  },
  getArticleList() {
    if (this.data.count < this.data.page * this.data.page_size) {
      ui.showToast("已经到底了~")
      return
    }
    var _this = this
    this.setData({
      articlePage: this.data.articlePage + 1
    })
    var page = { pageNo: _this.data.articlePage, pageSize: _this.data.articlePageSize }
    //发送请求
    httpUtils.request({
      method: "POST",
      showLoading: false,
      url: `/api/article/list`,
      // message:"正在创建...",
      data: page,
    }).then(res => {
      if (res.data.code != 0) {
        ui.showToast(res.data.mes)
      } else {
        let data = _this.data.articles
        for (let i = 0; i < res.data.data.pageData.length; i++) {
          data.push(res.data.data.pageData[i])
        }
        _this.setData({
          articles: data
        })
        console.log(_this.data.articles)
      }
    }).catch(err => {
      console.log(err)
    });
  },
  more() {
    wx.navigateTo({
      url: '../department/index',
    })
  },
  navigateToURL(e) {
    // console.log(e);
    let elements = this.data.elements;
    let navigateURL = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: '/pages/webview/index?url=' + navigateURL,
      success: (result) => { },
      fail: (res) => { },
      complete: (res) => { },
    })
  },
  booking() {
    wx.navigateTo({
      url: '../to_booking/index',
    })
  },
  test() {
    wx.navigateTo({
      url: '../to_test/index',
    })
  },
  guide() {
    wx.navigateTo({
      url: '../to_guide/index',
    })
  },
  consult() {
    wx.navigateTo({
      url: '../to_consult/index',
    })
  }
})
